package com.kzd.gtgame.main.home

import android.graphics.Color
import android.graphics.Rect
import android.view.View
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ConvertUtils
import com.google.android.material.appbar.AppBarLayout
import com.kzd.gtgame.R
import com.kzd.gtgame.custom.setOnClickAntiShakeListener
import com.kzd.gtgame.jetpack.vm.HomeViewModel
import com.kzd.gtgame.main.home.adapter.BargainFeaturedAdapter
import com.kzd.gtgame.main.home.adapter.BargainReleaseAdapter
import com.kzd.gtgame.main.toBargainBuyActivity
import com.kzd.gtgame.main.toBargainRecordActivity
import com.kzd.gtgame.main.toBargainSellActivity
import com.kzd.gtgame.main.toMainActivity
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_home_bargain.*

class HomeBargainActivity : OwnerViewModeActivity<HomeViewModel>(),
    AppBarLayout.OnOffsetChangedListener {

    private val mBargainFeaturedAdapter = BargainFeaturedAdapter()

    private val mBargainReleaseAdapter = BargainReleaseAdapter()

    override fun viewModel(): HomeViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_home_bargain

    override fun stateBar(): StatusBar {
        return StatusBar(color = R.color.white, view = flHomeBargainBar)
    }

    override fun initView() {

        mAppBarLayout.addOnOffsetChangedListener(this)

        rvHomeBargainFeaturedBody.apply {
            val layoutManager = LinearLayoutManager(context).apply {
                orientation = LinearLayoutManager.HORIZONTAL
            }
            this.layoutManager = layoutManager

            addItemDecoration(object : RecyclerView.ItemDecoration() {
                override fun getItemOffsets(
                    outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State
                ) {
                    val position: Int = parent.getChildAdapterPosition(view)
                    if (position == 0) {
                        outRect.left = ConvertUtils.dp2px(15f)
                    }
                    if (position == state.itemCount - 1) {
                        outRect.right = ConvertUtils.dp2px(15f)
                    } else {
                        outRect.right = ConvertUtils.dp2px(15f)
                    }
                }
            })
        }.adapter = mBargainFeaturedAdapter

        rvHomeBargainReleaseBody.adapter = mBargainReleaseAdapter

        //小号回收
        tvHomeBargainBuy.setOnClickAntiShakeListener {
            toBargainBuyActivity()
        }

        //我要卖号
        tvHomeBargainSell.setOnClickAntiShakeListener {
            toBargainSellActivity()
        }

        //交易记录
        tvHomeBargainRecord.setOnClickAntiShakeListener {
            toBargainRecordActivity()
        }

        //联系客服
        tvHomeBargainService.setOnClickAntiShakeListener {
            toMainActivity(index = 3)
        }

    }

    override fun initData() {
        viewModel.homeBargainFeatured()
        viewModel.homeBargainRelease()
    }

    override fun initViewMode() {

        viewModel.homeBargainFeaturedLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status == Status.FAILURE) {
                llHomeBargainFeatured.visibility = View.GONE
            } else if (response.status == Status.SUCCESS) {
                llHomeBargainFeatured.visibility = View.VISIBLE
            }
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            if (data.size == 0) {
                llHomeBargainFeatured.visibility = View.GONE
            }
            mBargainFeaturedAdapter.setNewInstance(data)
        })

        viewModel.homeBargainReleaseLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            mBargainReleaseAdapter.setNewInstance(data)
        })

    }

    override fun onOffsetChanged(appBarLayout: AppBarLayout?, verticalOffset: Int) {
        if (verticalOffset * -1 == mAppBarLayout.totalScrollRange) {
            llHomeBargainSearch.setBackgroundColor(Color.WHITE)
        } else {
            llHomeBargainSearch.setBackgroundColor(Color.TRANSPARENT)
        }
    }
}